package com.itheima.controller.interceptor;

import com.alibaba.fastjson.JSON;
import com.itheima.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
//@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();



        String[] urls=new String[]{"employee/login","employee/logout","login.html","/api/","/images/","/js/","/plugins/","/styles/"};
        Long empId = (Long) request.getSession().getAttribute("empId");
        if (empId!=null){
            return true;
        }

        for (String url : urls) {
            if (uri.contains(url)){
                return true;
            }
        }

       response.setContentType("application/json;charset=utf-8");
        String notlogin = JSON.toJSONString(R.error("NOTLOGIN"));
        response.getWriter().write(notlogin);

        return false;


    }
}
